<?php
/**
 * JoiEditor - Joostina WYSIWYG Editor
 *
 * Backend controller
 *
 * @version 1.0 beta 3
 * @package JoiEditor
 * @subpackage	Admin
 * @filename admin.joieditor.php
 * @author JoostinaTeam
 * @copyright (C) 2008-2010 Joostina Team
 * @license see license.txt
 *
 **/

defined('_VALID_MOS') or die();

require_once ($mainframe->getPath('class'));

$ids = mosGetParam($_REQUEST, 'cid', array(0));

switch($task) {

	default:
		configs_list($option);
		break;

	case 'new':
		config($option, 1);
		break;

	case 'config':
		config($option);
		break;

	case 'save_config':
		save_config($option);
		break;

	case 'cancel':
		cancel($option);
		break;

	case 'remove':
		remove($ids, $option);
		break;

	case 'info':
		info($option);
		break;

}

function cancel($option) {
	mosRedirect('index2.php?option='.$option);
}

function configs_list($option) {

	$configs = joiEditor::get_configs_list();

	$limit = intval(mosGetParam($_REQUEST, 'limit', 0));
	$limitstart = intval(mosGetParam($_REQUEST, 'limitstart', 0));
	require_once( JPATH_BASE. '/administrator/includes/pageNavigation.php');
	$pageNav = new mosPageNav(count($configs), $limitstart, $limit);

	include_once( JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_joieditor'.DS.'view'.DS.'config_list.php');

}

function config($option, $isNew = 0) {
	$database = &database::getInstance();
	$group = mosGetParam($_REQUEST,'group','default');

	mosCommonHTML::loadOverlib();

	$config = new joiEditor($database, 'joieditor', $group);
	$config->display_config($option, $isNew);
}

function save_config($option) {
	$database = &database::getInstance();
	$group = mosGetParam($_REQUEST,'group','default');

	$user_group = mosGetParam($_REQUEST,'gid','');
	if($user_group) {
		$group = $group.'_gid_'.$user_group;
	}

	$config = new joiEditor($database, 'joieditor', $group);
	$config->save_config();

	mosRedirect('index2.php?option='.$option, _CONFIG_SAVED);
}

function remove($ids, $option) {
	global $database;

	for($i = 0,$n = count ($ids); $i < $n; $i ++) {
		joiEditor::delete_config($ids[$i]);
	}

	mosRedirect('index2.php?option=' . $option , _C_JOIEDITOR_ADMIN_REMOVED);

}

function info($option) {
	include_once( JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_joieditor'.DS.'view'.DS.'info.php');
}